<template>
  <div class="app-container">
    <div class="search-form">
      <el-form ref="searchForm" :model="searchForm" size="mini" :inline="true">
        <div>
          <el-form-item label="产品名称">
            <el-input
              style="width: 200px"
              v-model="searchForm.name"
              placeholder="请输入产品名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="额度区间">
            <el-input
              style="width: 100px"
              v-model="searchForm.minQuota"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item>-</el-form-item>
          <el-form-item>
            <el-input
              style="width: 100px"
              v-model="searchForm.maxQuota"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="年化利率">
            <el-input
              style="width: 100px"
              v-model="searchForm.minAnnualRate"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item>-</el-form-item>
          <el-form-item>
            <el-input
              style="width: 100px"
              v-model="searchForm.maxAnnualRate"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="产品状态" v-if="activeName == 0">
            <el-select
              style="width: 200px"
              v-model="searchForm.publishStatus"
              placeholder="请选择"
            >
              <el-option label="全部" value="0"></el-option>
              <el-option label="出售中" value="1"></el-option>
              <el-option label="仓库中" value="2"></el-option>
            </el-select>
          </el-form-item>
          
        </div>
        <div>
          <!--          <el-form-item label="借款期限" prop="loanTerm">-->
          <!--            <el-select-->
          <!--              clearable-->
          <!--              style="width: 200px"-->
          <!--              v-model="searchForm.loanTerm"-->
          <!--              placeholder="请选择借款期限"-->
          <!--            >-->
          <!--              <el-option-->
          <!--                v-for="item in loanTermList"-->
          <!--                :key="item.value"-->
          <!--                :label="item.label"-->
          <!--                :value="item.value"-->
          <!--              >-->
          <!--              </el-option>-->
          <!--            </el-select>-->
          <!--          </el-form-item>-->
          <el-form-item label="来源">
              <el-select
                clearable
                style="width: 200px"
                v-model="searchForm.channelCode"
                placeholder="请选择贷款分类"
              >
                <el-option
                  v-for="item in product_channelList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          <el-form-item label="创建时间" prop="startTime">
            <el-date-picker
              style="width: 200px"
              v-model="searchForm.beginTime"
              align="right"
              type="date"
              placeholder="选择开始日期"
              :picker-options="pickerOptions"
              value-format="yyyy-MM-dd"
              format="yyyy 年 MM 月 dd 日"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item>-</el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="searchForm.endTime"
              align="right"
              type="date"
              placeholder="选择结束日期"
              :picker-options="pickerOptions"
              value-format="yyyy-MM-dd"
              format="yyyy 年 MM 月 dd 日"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button @click="resetList">重置</el-button>
            <el-button type="primary" @click="getlist('searchForm')"
              >查询</el-button
            >
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div style="margin: 20px 0; overflow-y: auto; height: 500px">
      <el-table
        :data="tableData"
        ref="tableDataRef"
        border
        size="mini"
        @selection-change="selectTableList"
        v-loading="listLoading"
        row-key="id"
      >
        <el-table-column
          :selectable="selecTableItem"
          type="selection"
          width="50"
        ></el-table-column>
        <el-table-column width="350" label="产品名称">
          <template slot-scope="scope">
            <div class="productNameStyle">
              <div class="image">
                <el-image
                  style="width: 80px; height: 80px"
                  :src="scope.row.pic"
                  :preview-src-list="[scope.row.pic]"
                >
                </el-image>
              </div>
              <div class="name-msg">
                <div style="color: #222; font-weight: bold">
                  {{ scope.row.name }}
                </div>
                <div style="color: #999">ID: {{ scope.row.id }}</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="产品所属银行">
          <template slot-scope="scope">
            <span>{{ scope.row.bankName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="额度区间">
          <template slot-scope="scope">
            <span>{{ scope.row.minQuota }}万 - {{ scope.row.maxQuota }}万</span>
          </template>
        </el-table-column>
        <el-table-column label="年化利率">
          <template slot-scope="scope">
            <span
              >{{ scope.row.minAnnualRate }}% -
              {{ scope.row.maxAnnualRate }}%</span
            >
          </template>
        </el-table-column>
        <el-table-column label="借款期限">
          <template slot-scope="scope">
            <span>{{ scope.row.loanTerm }}期</span>
          </template>
        </el-table-column>
        <el-table-column label="产品状态">
          <template slot-scope="scope">
            <span v-if="scope.row.publishStatus == 1">出售中</span>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="createTime"
          width="180"
        ></el-table-column>
      </el-table>
    </div>
    <el-pagination
      class="pageRight"
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[10, 20, 30, 40]"
      :current-page="searchForm.pageNum"
      :page-size="searchForm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <div class="select-btn">
      <el-button size="small" type="primary" @click="ok">确认选择</el-button>
    </div>
  </div>
</template>

<script>
import { getBankList } from "@/api/system";
import { getProductList } from "@/api/product";
import { loanTermList, pickerOptions } from "@/views/product/options";
import { timeFun } from "@/utils/date";
export default {
  name: "GoodList",
  props: {
    handleNum: {
      type: String,
      default: ""
    },
    checked: {
      type: Array,
      default: () => []
    }
  },
  filters: {},
  data() {
    return {
      activeName: "1",
      loanTermList: loanTermList,
      searchForm: {
        name: "",
        minQuota: "",
        maxQuota: "",
        minAnnualRate: "",
        maxAnnualRate: "",
        publishStatus: "1",
        beginTime: "",
        endTime: "",
        pageNum: 1,
        pageSize: 10,
        channelCode: ''
      },
      tableData: [],
      total: 0,
      listLoading: false,
      pickerOptions,
      checkBox: [],
      bankList: [],
      product_channelList: []
    };
  },
  async created() {
    this.product_channelList = await this.$getDictData("product_channel");
    await this.getBankList();
    await this.getlist();
  },
  methods: {
    getBankList() {
      getBankList().then(res => {
        if (res.code === 200) {
          this.bankList = res.data;
        }
      });
    },
    changeOne() {},
    changeType(v) {
      console.log(v);
    },
    ok() {
      if (this.handleNum != "many" && this.checkBox.length > this.handleNum) {
        return this.$message.warning(`只能选择${this.handleNum}个产品`);
      } else {
        const selectProduct = this.checkBox.map(item => {
          return item;
        });
        this.$emit("getStoreItem", selectProduct);
      }
      // this.$emit("getStoreItem", this.checkBox);
    },
    selecTableItem(row) {
      if (row.publishStatus === 1) {
        return true;
      }
    },
    resetList() {
      this.searchForm = {
        name: "",
        minQuota: "",
        maxQuota: "",
        minAnnualRate: "",
        maxAnnualRate: "",
        publishStatus: "1",
        beginTime: "",
        endTime: "",
        pageNum: 1,
        pageSize: 10
      };
      this.getlist();
    },

    selectTableList(val) {
      this.checkBox = val;
    },
    findbankName(id) {
      const obj = this.bankList.find(v => v.id == id);
      return obj.name;
    },
    getlist() {
      if (
        (this.searchForm.beginTime && !this.searchForm.endTime) ||
        (!this.searchForm.beginTime && this.searchForm.endTime)
      ) {
        return this.$message.warning("请填写开始日期或者结束日期哦");
      }
      if (this.searchForm.beginTime && this.searchForm.endTime) {
        if (!timeFun(this.searchForm.beginTime, this.searchForm.endTime)) {
          return this.$message.warning("结束时间必须大于开始时间");
        }
      }

      this.listLoading = true;
      getProductList(this.searchForm)
        .then(res => {
          res.data.list.forEach(v1 => {
            // v1.bankName = this.findbankName(v1.bankId);
            // v1.matchType = this.datafun("matchType", v1.matchType.split(","));
            // v1.loanType = this.datafun("loanType", [v1.loanType]);
            // v1.loanTerm = this.$dictTypeFun(v1.loanTerm, "product_cid");
          });
          this.tableData = res.data.list;
          this.total = res.data.total;
          this.$nextTick(() => {
            this.toggleRowSelection();
          });
          this.listLoading = false;
        })
        .finally(() => {
          this.listLoading = false;
        });
    },

    // 默认绑定多选的值
    toggleRowSelection() {
      if (this.checked) {
        let checkedList = [];
        this.checked.forEach(v => {
          this.tableData.forEach(v1 => {
            if (v === v1.id) {
              checkedList.push(v1);
            }
          });
        });
        //回填数据
        checkedList.forEach(v => {
          this.$refs.tableDataRef.toggleRowSelection(v, true);
        });
        this.selectList = checkedList;
      }
    },
    handleClick(val) {
      this.searchForm.publishStatus = val.index;
      this.getlist();
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val;
      this.getlist();
    },
    handleCurrentChange(val) {
      this.searchForm.pageNum = val;
      this.getlist();
    }
  }
};
</script>

<style scoped lang="scss">
.app-container {
  height: 400px;
  position: relative;
  padding: 20px 0;
}
.form-box {
  background-color: #fff;
  padding: 20px;
}

.productNameStyle {
  display: flex;
  height: 100px;
  .image {
    width: 100px;
    height: 100px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .name-msg {
    margin-left: 10px;
    padding: 10px 0;
    height: 80px;
    display: flex;
    justify-content: space-between;
    flex-direction: column;
  }
}
/deep/ .disable-select-row {
  pointer-events: none; /* 禁止鼠标事件 */
  user-select: none; /* 禁止文本选择 */
  // background-color: #f5f7fa; /* 设置背景色为灰色 */
}
.select-btn {
  text-align: right;
  margin: 10px;
}
</style>
